package com.taxshare.seller.aspect;

import com.alibaba.fastjson.JSON;
import com.taxshare.seller.service.InvoiceLogService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName: LogAspect
 * @Description: 接口调用日志
 * @author: luozhongyi
 * @date: 2021/4/12 13:43
 */
@Aspect
@Slf4j
@Component
public class SellerLogAspect {

    @Autowired
    InvoiceLogService invoiceLogService;

    @Pointcut("@annotation(com.taxshare.seller.aspect.SLog)")
    public void addAdvice() {
        log.info("========加载日志========");
    }

    @Around("addAdvice()")
    public Object Interceptor(ProceedingJoinPoint point) throws Throwable {
        // 记录开始时间
        long starttime = System.currentTimeMillis();
        String method = point.getSignature().getName();
        Object args = point.getArgs();
        String reqParam = JSON.toJSONString(args);
        Object result;
        try {
            result = point.proceed();
        } catch (Exception e) {
            result = e;
        }
        int time = (int) (System.currentTimeMillis() - starttime);
        String resultJson = JSON.toJSONString(result);
        try {
            invoiceLogService.insertInterfaceLog(method, reqParam, resultJson, time);
        }catch (Exception e){
            log.error("保存切面日志信息错误",e);
        }
        return result;
    }
}
